package com.fwzc.giuseppe.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.fwzc.giuseppe.config.Config;
import com.fwzc.giuseppe.util.Utils;
import com.fwzc.giuseppe.view.BasePagerAdapter;
import com.lidroid.xutils.util.LogUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * @author : 
 * @date   : 
 * @desc   : 广告条适配器
 */
public class BannerAdapter extends BasePagerAdapter {
	private Context mContext;
	private List<BannerInfo> mInfos;
//	private int mWidth;
//	private int mHeight;
	
	public BannerAdapter(Context context, List<BannerInfo> infos) {
		this.mContext = context;
		this.mInfos = infos; 
//		this.mWidth = DeviceUtil.getDevice(context).getWidth();
//		this.mHeight = context.getResources().getDimensionPixelSize(120);
	}

	@Override
	public int getCount() {
		return mInfos==null? 0: mInfos.size();
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view == (View) object;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		container.removeView((View) object);
	}

	@Override
	public Object instantiateItem(ViewGroup container, final int position) {
		final BannerInfo info = mInfos.get(position);
		ImageView asyncImgBanner = new ImageView(mContext);
		asyncImgBanner.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(!TextUtils.isEmpty(info.mUrl)){
					Uri uri = Uri.parse(info.mUrl);
					LogUtils.d("----------------mUrl="+info.mUrl);
					Intent it = new Intent(Intent.ACTION_VIEW, uri);
					mContext.startActivity(it);
				}
			}
		});
	
		asyncImgBanner.setScaleType(ScaleType.FIT_XY);
        container.addView(asyncImgBanner);
        if(!TextUtils.isEmpty(info.mImgUrl)&&Utils.isUrl(info.mImgUrl)){
        	ImageLoader.getInstance().displayImage(info.mImgUrl, asyncImgBanner,Config.no_iv_options);
        } 
		return asyncImgBanner;
	}
}
